package stone.ast;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import runner.Environment;
import stone.StoneException;
import stone.Token;

import java.util.ArrayList;
import java.util.Iterator;

public class ASTLeaf extends ASTree {
  private static ArrayList<ASTree> empty = new ArrayList<>();
  protected Token token;

  public ASTLeaf(Token t) {
    token = t;
  }

  public Token getToken() {
    return token;
  }

  @Override
  public ASTree child(int i) {
    throw new IndexOutOfBoundsException();
  }

  @Override
  public int numChildren() {
    return 0;
  }

  @Override
  public Iterator<ASTree> children() {
    return empty.iterator();
  }

  @Override
  public String location() {
    return "at line" + token.getLineNumber();
  }

  @NotNull
  @Override
  public Object eval(@Nullable Environment env) {
    throw new StoneException("cannot eval: " + toString(), this);  }

  @Override
  public String toString() {
    return token.getText();
  }
}
